<?php

namespace framework\system\kernel;

use framework;

class Application extends Component {

    protected $defaultController = "Index";
    protected $defaultAction = "Index";
    protected $controllerId = "Index";
    protected $actionId = "Index";
    protected $controllerPath = null;
    protected $controller;

    /*
     * 进行初始化
     */

    public function init() {
        ;
    }

    /*
     * 跑起来，压入事件。
     */

    public function start() {
        framework::em()->push("event.application.getControllerId");
        framework::em()->push("event.application.getActionId");
        framework::em()->push("event.application.createController");
        framework::em()->push("event.application.runController");
    }

    public function end() {
        ;
    }

    final public function createController() {

        //$this->getControllerId();
        if ($this->controllerId) {

            $this->controllerId = ucwords($this->controllerId);
            $controller_class = 'myapp' . $this->controllerId;
            if (CFile::requireFile(DIR_APPLICATION . DS . "controller" . DS . $this->controllerPath . DS . $this->controllerId . EXT)) {
                return $this->controller = new $controller_class;
            } elseif (_DEBUG) {
                _die("ERROR : CONTROLLER { $this->controllerPath / $this->controllerId } NOT FOUND!");
            } else {
                $this->controllerId = $this->getDefaultController();
                $this->controllerPath = '';
                framework::em()->prev();
            }
        }
    }

    final public function runController() {

        //$this->getActionId();
        if (is_object($this->controller)) {
            $this->controller->actionId = $this->actionId;
            if (method_exists($this->controller, 'action' . ucwords($this->actionId))) {
                $this->controller->action();
            } else if (_DEBUG) {
                _die("ERROR : ACTION { $this->actionId } NOT FOUND!");
            } else {
                $this->actionId = $this->getDefaultAction();
                framework::em()->prev();
            }
        }
    }

    public function getControllerId() {
        $this->controllerId = framework::mm()->get('message.application.controllerId');


        if ($this->controllerId) {
            $controllerId = $this->controllerId;
            $controllerId = explode('/', $controllerId);
            $this->controllerId = array_pop($controllerId);
            $this->controllerPath = implode('/', $controllerId);
            return $this->controllerId;
        }
        $this->controllerId = $this->getDefaultController();

        if ($this->controllerId) {
            return $this->controllerId;
        }
        $this->controllerId = $this->defaultController;
        return $this->controllerId;
    }

    public function getActionId() {
        $this->actionId = framework::mm()->get('message.application.actionId');
        if ($this->actionId) {
            return $this->actionId;
        }
        $this->actionId = $this->getDefaultAction();
        if ($this->actionId) {
            return $this->actionId;
        }
        $this->actionId = $this->defaultAction;
        return $this->actionId;
    }

    public function getDefaultController() {
        if (framework::cm()->get("com.config")->get("application.defaultControllerId")) {
            $this->defaultController = framework::cm()->get("com.config")->get("application.defaultControllerId");
        }
        return $this->defaultController;
    }

    public function getDefaultAction() {
        if (framework::cm()->get("com.config")->get("application.defaultActionId")) {
            $this->defaultAction = framework::cm()->get("com.config")->get("application.defaultActionId");
        }

        return $this->defaultAction;
    }

    public function getErrorController() {
        return framework::cm()->get("com.config")->get("application.errorControllerId");
    }

}
